home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / MACSHELL / MS1 / COMMANDS / WORDCOUN.C < prev   
Text File  |  1992-12-02  |  7KB  |  287 lines

  1. /*
  2.  *    MacShell Source File
  3.  *
  4.  *    Copyright (c) 1989, 1990, 1991, 1992  Suick Bay Technologies.  All rights reserved.
  5.  *
  6.  *
  7.  *    RESTRICTIONS ON MacShell program and source code.
  8.  *
  9.  *    Ñ╩MacShell¬ is a product of Suick Bay Technologies and is provided for
  10.  *    restricted use by the owner of the CDROM "Disk to the future II".
  11.  *
  12.  *    Ñ╩No permission is granted for any commercial use without the written
  13.  *    consent of the Suick Bay Technologies.
  14.  *
  15.  *    Ñ╩No permission is granted for any redistribution of any kind use without
  16.  *    the written consent of the Suick Bay Technologies.
  17.  *
  18.  *    Ñ╩Permission is granted to use this for any personal noncommercial use.
  19.  *
  20.  *    Ñ╩You may not distribute source or executable code at all, nor may you 
  21.  *    distribute it with or within a commercial product without the written
  22.  *    consent of the Suick Bay Technologies.  Please send modifications to 
  23.  *    the author for inclusion in updates to the program.  Thanks.
  24.  *
  25.  *
  26.  *    MacShell¬ IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  27.  *    WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  28.  *    PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  29.  *
  30.  *    SUICK BAY TECHNOLOGIES SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  31.  *    INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY MACSHELL
  32.  *    OR ANY PART THEREOF. 
  33.  *
  34.  *    In no event will Suick Bay Technologies be liable for any lost revenue
  35.  *    or profits or other special, indirect and consequential damages, even if
  36.  *    Suick Bay Technologies has been advised of the possibility of such damages.
  37.  *
  38.  *    Suick Bay Technologies can be reached at:
  39.  *    
  40.  *    8768 Cottonwood lane
  41.  *    Maple Grove, MN 55369
  42.  *    Voice: (612) 425-7025
  43.  *    AppleLink: D5233
  44.  *    
  45.  *
  46.  *    No parts of this software may be reproduced or stored in a
  47.  *    retrieval system or transmitted in any form, or any means,
  48.  *    electronic, mechanical, photocopying, recording or otherwise,
  49.  *    without the prior written permission of Suick Bay Technologies.
  50.  *    
  51.  *    Spread the word and not the disk.
  52.  *    
  53.  *    SPK 012290    :    Initial
  54.  */
  55.  
  56. #include    "SystemPub.h"
  57. #include    "Proc.h"
  58. #include    "ShellPub.h"
  59. #include    "Path.h"
  60.  
  61. #define        BUFSIZE        256
  62.  
  63. /*******************************************************************
  64.  *
  65.  *    Function WC
  66.  *
  67.  *    PathName Callback function
  68.  *
  69.  *    usage wc [options] [names]    
  70.  *
  71.  *******************************************************************/
  72.  
  73. #define        charCnt        (**MyShell).Proc[ProcID].long0
  74. #define        wordCnt        (**MyShell).Proc[ProcID].long1
  75. #define        lineCnt        (**MyShell).Proc[ProcID].long2
  76. #define        fileCnt        (**MyShell).Proc[ProcID].long3
  77.  
  78. DoCount( char *str, WHandle ShellWh, int16 ProcID )
  79. {
  80. char            *sp, last;
  81. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  82.  
  83.     sp = str;
  84.     while( *sp )
  85.         {
  86.         if( *sp == '\n' || *sp == '\r' )
  87.             {
  88.             charCnt++;
  89.             lineCnt++;
  90.             if( last != ' ' )
  91.                 wordCnt++;
  92.                 
  93.             last = *sp++;
  94.             }
  95.         else if( *sp == ' ' )
  96.             {
  97.             while( (last = *sp++) == ' ' )
  98.                 charCnt++;
  99.             wordCnt++;
  100.             }
  101.         else
  102.             {
  103.             charCnt++;
  104.             last = *sp++;
  105.             }
  106.         }
  107. }
  108.  
  109. /*******************************************************************/
  110.  
  111. Boolean        wcLine, wcChar, wcWord, wcFiles, wcCheckArgs, wcHasFiles;
  112.  
  113. PrintWCCount( WHandle ShellWh, int16 ProcID )
  114. {
  115. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  116.  
  117.     if( !wcLine && !wcChar && !wcWord )
  118.         {
  119.         wcLine = TRUE;
  120.         wcChar = TRUE;
  121.         wcWord = TRUE;
  122.         }
  123.         
  124.     if( wcChar )
  125.         procPrintf( ShellWh, ProcID, "   Chars : %ld\n", charCnt );
  126.     if( wcWord )
  127.         procPrintf( ShellWh, ProcID, "   Words : %ld\n", wordCnt );
  128.     if( wcLine )
  129.         procPrintf( ShellWh, ProcID, "   Lines : %ld\n", lineCnt );
  130.     if( wcFiles )
  131.         procPrintf( ShellWh, ProcID, "   Files : %ld\n", fileCnt );
  132. }
  133.  
  134. /*******************************************************************/
  135.  
  136. void        WCCallBack( WHandle ShellWh, int16 ProcID, char *path, char *last,
  137.                         pathType what, int16 vRefNum, int32 dirID )
  138. {
  139. char        readBuf[ BUFSIZE ];
  140. char        str[ 256 ];
  141. int16        err, fileRefNum;
  142. FInfo        finder;
  143. int32        count = BUFSIZE - 1;
  144. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  145.  
  146.     if( what == pathIsFile )
  147.         {
  148.         strcpy( str, last );
  149.         CtoPstr( str );
  150.     
  151.         err = HGetFInfo( vRefNum, dirID, str, &finder );
  152.         
  153.         if( !err && (finder.fdType == 'TEXT'))
  154.             {
  155.             err = HOpen( vRefNum, dirID, str, fsRdPerm, &fileRefNum );
  156.  
  157.             while( !err && !UserAbort() )
  158.                 {
  159.                 err = FSRead( fileRefNum, &count, readBuf );
  160.                 readBuf[ count ] = '\0';
  161.                 
  162.                 if( !err || err == eofErr )
  163.                     DoCount( readBuf, ShellWh, ProcID );
  164.                 }
  165.     
  166.             if( fileRefNum )    /* if a file open, close it */
  167.                 FSClose( fileRefNum );
  168.             }
  169.         }
  170. }
  171.  
  172. /*******************************************************************/
  173.  
  174. void        WCFile( WHandle ShellWh, int16 ProcID, char *argument )
  175. {
  176. ShellWindRec    **MyShell;
  177. int16            m;
  178.  
  179.     MyShell = (ShellWindRec **) (**ShellWh).thing;
  180.  
  181.     m = ExpandPath( ShellWh, ProcID, argument, (ProcPtr) WCCallBack,
  182.             (**MyShell).pwdVRefNum, (**MyShell).pwdDirID );
  183.  
  184.     if( wcFiles )
  185.         fileCnt += (long) m;
  186.         
  187.     ResetShellPWD( ShellWh );
  188. }
  189.  
  190. /*******************************************************************/
  191.  
  192. Boolean            DoWC( int16 ProcToken, WHandle ShellWh, int16 ProcID, char *string )
  193. {
  194. int16            i, argc;
  195. char            *cp, argument[ 256 ];
  196. ShellWindRec    **MyShell = (ShellWindRec **) (**ShellWh).thing;
  197.  
  198.     switch( ProcToken )
  199.         {
  200.         case    PROC_INIT    :
  201.             charCnt = 0;    wordCnt = 0;    lineCnt = 0;
  202.             wcLine = FALSE;    wcChar = FALSE;    wcWord = FALSE;
  203.             wcCheckArgs = TRUE; wcHasFiles = FALSE;
  204.             (**MyShell).Proc[ ProcID ].flags = TRUE;
  205.             break;
  206.             
  207.         case    PROC_TERM    :
  208.         case    PROC_BREAK    :
  209.             PrintWCCount( ShellWh, ProcID );
  210.             
  211.             /* Tell the shell that we're done */
  212.             SendOutToken( ShellWh, ProcID, PROC_BREAK );
  213.             
  214.             /* Turn ourself off */
  215.             (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  216.             (**MyShell).Proc[ ProcID ].flags = FALSE;        
  217.             break;
  218.             
  219.         case    PROC_STDIN    :
  220.             if( (**MyShell).Proc[ ProcID ].flags )
  221.                 {
  222.                 if( wcCheckArgs )
  223.                     {
  224.                     wcCheckArgs = FALSE;
  225.                     fileCnt = 0L;
  226.                     
  227.                     /* get arguments */
  228.                     wcFiles = FALSE;
  229.                     argc = (**MyShell).Proc[ ProcID ].argc;
  230.                     if( argc > 1 )
  231.                         {
  232.                         for( i = 1; i < argc; i++ )
  233.                             {
  234.                             GetArgv( ShellWh, ProcID, i, argument );
  235.                             cp = argument;
  236.                 
  237.                             if( *cp++ == '-' )
  238.                                 {
  239.                                 while( *cp )
  240.                                     switch( *cp++ )
  241.                                         {
  242.                                         case    'c'    :    /* count characters */
  243.                                             wcChar = TRUE;
  244.                                             break;
  245.                                         case    'l'    :    /* count lines        */
  246.                                             wcLine = TRUE;
  247.                                             break;
  248.                                         case    'w'    :    /* count words        */
  249.                                             wcWord = TRUE;
  250.                                             break;
  251.                                         case    'f'    :    /* file count        */
  252.                                             wcFiles = TRUE;
  253.                                             break;
  254.                                         }
  255.                                 }
  256.                             else
  257.                                 wcHasFiles = TRUE;
  258.                             }
  259.                         }
  260.                     }
  261.                     
  262.                 if( wcHasFiles )
  263.                     {
  264.                     (**MyShell).Proc[ ProcID ].flags = FALSE;        
  265.                     for( i = 1; i < (**MyShell).Proc[ ProcID ].argc; i++ )
  266.                         {
  267.                         GetArgv( ShellWh, ProcID, i, argument );
  268.                         if( *argument != '-' )
  269.                             WCFile( ShellWh, ProcID, argument );
  270.                         }
  271.     
  272.                     PrintWCCount( ShellWh, ProcID );
  273.                     
  274.                     /* Tell the shell that we're done */
  275.                     SendOutToken( ShellWh, ProcID, PROC_BREAK );
  276.                     /* Turn ourself off */
  277.                     (**MyShell).Proc[ ProcID ].ProcActive = FALSE;
  278.                     }
  279.                 else
  280.                     DoCount( string, ShellWh, ProcID );
  281.                 }
  282.  
  283.             return( FALSE );
  284.         }
  285. }
  286.  
  287.